//
//  HomeV.m
//  BCW
//
//  Created by Mac on 16/3/28.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "HomeV.h"
#import "CountResoureVC.h"
#import "HistoryLotteryViewController.h"
#import "MoreTableVC.h"
#import "RrecommendVC.h"
#import "DateManager.h"
#import "HBWNetDataManager.h"
#import "ProtocolViewController.h"

#define StrMgring @"     "

@interface HomeV()<UINavigationControllerDelegate>

@property (weak, nonatomic) IBOutlet UILabel *titleL;
@property (weak, nonatomic) IBOutlet UILabel *oneL;
@property (weak, nonatomic) IBOutlet UILabel *twoL;
@property (weak, nonatomic) IBOutlet UILabel *threeL;
@property (weak, nonatomic) IBOutlet UILabel *fourL;
@property (weak, nonatomic) IBOutlet UILabel *fiveL;

@property (weak, nonatomic) IBOutlet UILabel *timeTitleL;





@property (nonatomic, assign) BCWDate   date;

@end
@implementation HomeV


+ (instancetype)homeviewFormXib
{
    
    return [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:nil options:nil] lastObject];
}



- (instancetype)initWithCoder:(NSCoder *)aDecoder{
    if (self = [super initWithCoder:aDecoder]) {
//        [self addObserver:DateMgr forKeyPath:@"_bcwDate" options:NSKeyValueObservingOptionNew context:nil];
        
        
        [DateMgr addObserver:self forKeyPath:@"bcwDate" options:NSKeyValueObservingOptionNew context:nil];
        
    }
    return self;
}

- (void)dealloc{
    [self removeObserver:DateMgr forKeyPath:@"bcwDate"];
}

//历史开奖
- (IBAction)historylottery {
    HistoryLotteryViewController * historyLotteryV = [[HistoryLotteryViewController alloc]init];
    historyLotteryV.lotteryModel = self.model;
    UINavigationController *nva = [[UINavigationController alloc]initWithRootViewController:historyLotteryV];
    [self.viewController presentViewController:nva animated:YES completion:nil];
}
//推荐应用
- (IBAction)commend {
    RrecommendVC *recommend = [[RrecommendVC alloc] init];
    UINavigationController  *nav = [[UINavigationController  alloc   ] initWithRootViewController:recommend];
    [self.viewController presentViewController:nav animated:YES completion:nil];
}
//统计资讯
- (IBAction)countResoure {
    CountResoureVC *countResoureVC = [[CountResoureVC alloc]init];
    countResoureVC.lotteryModel = self.model;
    UINavigationController *nva = [[UINavigationController alloc]initWithRootViewController:countResoureVC];
    [self.viewController presentViewController:nva animated:YES completion:nil];
}

- (IBAction)moreBtn {
    MoreTableVC *more = [[UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:@"MoreTable"];
    UINavigationController *navVC = [[UINavigationController alloc] initWithRootViewController:more];
    [self.viewController presentViewController:navVC animated:NO completion:nil];
}



- (void)setModel:(LotteryStatusResultModel *)model{
    _model = model;
    
    // 本期开奖
    NSInteger phase = [[model.open_phase substringFromIndex:8] integerValue];
    _titleL.text = [NSString stringWithFormat:@"第%li期最新开奖:", phase];
    
    // 下期
    _timeTitleL.text = [NSString stringWithFormat:@"距离第%li期开奖剩余:", phase + 1];
    
    // 开奖结果
//    int i = 0;
    _oneL.font = BoldFont(20.f);
    _twoL.font = BoldFont(20.f);
    _threeL.font = BoldFont(20.f);
    _fourL.font = BoldFont(20.f);
    _fiveL.font = BoldFont(20.f);
    _oneL.text = [model.open_result substringWithRange:NSMakeRange(0, 1)];
    _twoL.text = [model.open_result substringWithRange:NSMakeRange(2, 1)];
    _threeL.text = [model.open_result substringWithRange:NSMakeRange(4, 1)];
    _fourL.text = [model.open_result substringWithRange:NSMakeRange(6, 1)];
    _fiveL.text = [model.open_result substringWithRange:NSMakeRange(8, 1)];
    
    // time
    _date = [DateMgr computeTimeDifferenceCrtTimeStr:model.now nextStr:model.next_time];
    
    _timeL.backgroundColor = [UIColor clearColor];
    _timeL.text = [NSString stringWithFormat:@"%.2li%@%.2li", _date.m,StrMgring, _date.s];
    
    [DateMgr beginTime];
}



- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context{
    
        _timeL.text = [NSString stringWithFormat:@"%.2li%@%.2li", DateMgr.bcwDate.m,StrMgring,  DateMgr.bcwDate.s];
}
- (IBAction)Potoctor:(UIButton *)sender {
   
    
    ProtocolViewController *protocolVC = [[ProtocolViewController alloc]init];
    UINavigationController *nva = [[UINavigationController alloc]initWithRootViewController:protocolVC];
    [self.viewController presentViewController:nva animated:YES completion:nil];


}


//- (void)changeTime:(NSTimer *)timer{
//    
//    if (_date.s == 0) {
//        
//        if (_date.m == 0 ) {
//            // 发送通知
//            NSLog(@"倒计时结束");
//        }
//        _date.s = 60;
//        _date.m -= 1;
//    }
//    
//    _date.s --;
//    _timeL.text = [NSString stringWithFormat:@"%.2li    %.2li", _date.m, _date.s];
//}

@end
